package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         202,
		Discription: "快乐数",
		Level:       1,
		Labels: map[string]int{
			"快慢指针": 1,
		},
	})
}

func IsHappy(n int) bool {
	next := func(num int) int {
		res := 0
		for num != 0 {
			last := num % 10
			res += last * last
			num /= 10
		}

		return res
	}

	fast := n
	slow := n
	//为什么得有next(fast) != 1：防止fast经过一轮就变1了，slow经过两轮变1，误认为陷入循环
	for fast != 1 && next(fast) != 1 {
		fast = next(next(fast))
		slow = next(slow)
		if fast == slow {
			return false
		}
	}

	return true
}